from pyspades.server import block_action
from pyspades.collision import distance_3d_vector
from commands import add, admin
from map import Map
from pyspades.constants import *
import commands

@admin
def startprefab(connection):
        connection.prefabing = 1
        connection.pasting = False
        return 'Place first block to start building object'
add(startprefab)
@admin
def endprefab(connection):
        connection.prefabing = 0
        connection.pasting = False
        return 'Object saved.'
add(endprefab)
@admin
def paste(connection, value = 1):
        if (0,0,0) in connection.prefabblocks:
            connection.pasting = int(value)
            connection.prefabing = 0
            return 'Place first block to paste object'
        else:
            return 'You have not created a prefab yet!'
add(paste)

def apply_script(protocol, connection, config):
    class PrefabConnection(connection):
        prefabing = 0
        prefaboffsetx = 0
        prefaboffsety = 0
        prefaboffsetz = 0
        prefaborientation = 0
        newprefabblock = (0,0,0)
        prefabblocks = set()
        pasting = False
        def on_block_build(self, x, y, z):
            if self.prefabing == 1:
                self.prefaboffsetx = x
                self.prefaboffsety = y
                self.prefaboffsetz = z
                self.prefaborientation = self.get_p_orientation()
                self.prefabblocks.clear()
                self.prefabblocks.add((0,0,0))
                self.prefabing = 2
            elif self.prefabing == 2:
                self.newprefabblock = (x - self.prefaboffsetx,y - self.prefaboffsety,z - self.prefaboffsetz)
                self.prefabblocks.add(self.newprefabblock)
                self.prefabing = 2
            elif self.pasting > 0:
                temp_x = int()
                for pasteblock in self.prefabblocks:
                    (self.prefaboffsetx, self.prefaboffsety, self.prefaboffsetz) = pasteblock
                    if self.prefaborientation == self.get_p_orientation():
                        self.prefaborientation = self.prefaborientation
                    elif self.prefaborientation - self.get_p_orientation() == 180 or self.prefaborientation - self.get_p_orientation() == -180:
                        self.prefaboffsetx *= -1
                        self.prefaboffsety *= -1
                    elif self.prefaborientation - self.get_p_orientation() == -270 or self.prefaborientation - self.get_p_orientation() == -90:
                        temp_x = self.prefaboffsetx
                        self.prefaboffsetx = self.prefaboffsety
                        self.prefaboffsety = temp_x * -1
                    elif self.prefaborientation - self.get_p_orientation() == 270 or self.prefaborientation - self.get_p_orientation() == 90:
                        temp_x = self.prefaboffsetx
                        self.prefaboffsetx = self.prefaboffsety * -1
                        self.prefaboffsety = temp_x
                    self.prefaboffsetx += x
                    self.prefaboffsety += y
                    self.prefaboffsetz += z
                    self.newprefabblock = (self.prefaboffsetx, self.prefaboffsety, self.prefaboffsetz)
                    self.prefab_place_block(self.newprefabblock)
                self.pasting = self.pasting - 1
                self.send_chat('Object Pasted. %s pastes left' % self.pasting)
            return connection.on_block_build(self, x, y, z)

        def prefab_place_block(self,newblock):
            (x,y,z) = newblock
            block_action.x = x
            block_action.y = y
            block_action.z = z
            block_action.player_id = self.player_id
            block_action.value = BUILD_BLOCK
            self.protocol.send_contained(block_action, save = True)
            self.protocol.map.set_point(block_action.x, block_action.y, block_action.z, self.color+(255,), user = False)

        def get_p_orientation(self):
            dir = int()
            dir = int(round(self.world_object.orientation.x)) + (int(round(self.world_object.orientation.y))*2)
            if dir == -2:
                return 180
            if dir == 1:
                return 90
            if dir == 2:
                return 0
            if dir == -1:
                return -90
            return 0
    return protocol, PrefabConnection